package com.jd.mti.beetl.test.controller;

import com.jd.mti.beetl.test.DataUtil;
import com.jd.mti.beetl.test.load.DataMan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.WebAsyncTask;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.Callable;

/**
 * @Author sizegang
 * @Date 2021/11/10 9:59
 * @Version 1.0
 */
@Controller
@RequestMapping("render")
public class RenderController {


    @RequestMapping("/testBeetl")
    public String testBeetl(HttpServletRequest request) {
        Map<String, Object> dataManMap = DataUtil.dataManMap;
        Map<String, Object> resp = new HashMap<>();

        for (Map.Entry<String, Object> entry : dataManMap.entrySet()) {

//            resp.put("a" + entry.getKey().toString(), entry.getValue());
            request.setAttribute("a" + entry.getKey().toString(), entry.getValue());
        }
//        modelAndView.addAllObjects(resp);

//        for (Map.Entry<String, Object> entry : dataManMap.entrySet()) {
//            modelAndView.addObject( "a" +entry.getKey().toString() , entry.getValue());
//        }


        return "beetl.html";
    }

    @RequestMapping("/testBeetlAsync")
    public WebAsyncTask<ModelAndView> testBeetlAsync() {
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> dataManMap = DataUtil.dataManMap;
        Map<String, Object> resp = new HashMap<>();

        Callable<ModelAndView> callable = new Callable<ModelAndView>() {
            @Override
            public ModelAndView call() throws Exception {
                for (Map.Entry<String, Object> entry : dataManMap.entrySet()) {

                    resp.put("a" + entry.getKey().toString(), entry.getValue());
                }
                modelAndView.addAllObjects(resp);

//        for (Map.Entry<String, Object> entry : dataManMap.entrySet()) {
//            modelAndView.addObject( "a" +entry.getKey().toString() , entry.getValue());
//        }


                modelAndView.setViewName("beetl.html");
                return modelAndView;

            }
        };
        WebAsyncTask<ModelAndView> webAsyncTask = new WebAsyncTask<ModelAndView>(callable);
        return webAsyncTask;
    }



    @RequestMapping("/emptyBeetl")
    public ModelAndView empty(@RequestParam(value = "loadData", required = false)Integer loadData) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("emptyBeet.html");
        return modelAndView;
    }



    @RequestMapping("/emptyFreemarker")
    public ModelAndView emptyFreemarker(@RequestParam(value = "loadData", required = false)Integer loadData) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("emptyFreemarker");
        return modelAndView;
    }





    @RequestMapping("/testFreemarker")
    public ModelAndView testFreemarker() {
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> dataManMap = DataUtil.dataManMap;
        for (Map.Entry<String, Object> entry : dataManMap.entrySet()) {
            modelAndView.addObject("a" + entry.getKey().toString(), entry.getValue());
        }
        modelAndView.setViewName("freemarker");
        return modelAndView;
    }


    private void insertData(List<DataMan> renderList1, Random random, int i) {
        int age = random.nextInt(i);
        DataMan dataMan = new DataMan(String.valueOf(age) + "-name", age);
        renderList1.add(dataMan);
    }


    @RequestMapping("/openresty")
    @ResponseBody
    public Map<String, List> openresty(@RequestParam(value = "loadData", required = false) Integer loadData) {
        return DataUtil.openRestyData;
    }

}
